package com.luck.easyfoodbatch.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.util.HttpUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InformationDetailsActivity extends BaseActivity {

    @BindView(R.id.wb)
    WebView wb;
    @BindView(R.id.mProgressBar)
    ProgressBar mProgressBar;
    private String fid = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information_details, "资讯详情", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        fid = getIntent().getStringExtra("fid");
        initView();
    }

    private void initView() {
        wb.getSettings().setDefaultTextEncodingName("utf-8");
        wb.getSettings().setJavaScriptEnabled(true);
        wb.getSettings().setUseWideViewPort(true);//设定支持viewport

        wb.getSettings().setLoadWithOverviewMode(true);

        wb.getSettings().setBuiltInZoomControls(true);

        wb.getSettings().setSupportZoom(true);//设定支持缩放
        wb.getSettings().setDisplayZoomControls(false);
        wb.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == mProgressBar.getVisibility()) {
                        mProgressBar.setVisibility(View.VISIBLE);
                    }
                    mProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        wb.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

        });
        wb.loadUrl(HttpUtil.NEWS_DETAIL + fid);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (wb != null) {
            wb.destroy();
        }
    }
}
